***********
clear
clear matrix
clear mata
set maxvar 20000 
set more off
************



global dir0="DATA/"
global dir1="TABLES/"
global dir3="PLOTS/"

log using "${dir3}log_plots.log", replace   


*** DEFINE GLOBALS
global demo1 "pop_2001 sh_pop_20_34_01 sh_pop_more urbanization unempl2001"
global demo2 "share_diploma distcapo diffworkerspc_96 diffimpresepc_96 firms_pc_01 workers_pc_01"



clear

*************************************************************
*							PLOTS						*
*************************************************************

************************************************************************************
* Figure 1: Evolution of Broadband Access and Penetration in Italian Municipalities
************************************************************************************

****************************************************
* Figure 2: MAP
****************************************************

*See the readme file for information on the source of the confidential ADSL municipal data.

************************************************************
* Figure 3: ADSL Coverage (2006) by Distance to Closest UGS
************************************************************

*Source: Between (2006, 2008) for ADSL access, and Between (2006, 2008) 
* and AGCOM (2008-2012a) for Internet penetration.

****************************************
*Figure 4: Difference in Turnout between early- and late-Internet municipalities
****************************************
use "${dir0}Data_national_regs.dta", clear

local years "1996 2001 2006 2008 2013"
foreach x of local  years{
egen NA_`x'=count(turnout) if year==`x' & adsl2005==5
egen NAf_`x'=min(NA_`x') if year==`x'
egen NB_`x'=count(turnout) if year==`x' & adsl2008==0
egen NBf_`x'=min(NB_`x') if year==`x'
egen sdA_`x'=sd(turnout) if year==`x' & adsl2005==5
egen sdB_`x'=sd(turnout) if year==`x' & adsl2008==0
egen sdAf_`x'=min(sdA_`x') if year==`x'
egen sdBf_`x'=min(sdB_`x') if year==`x'
gen sdAB_`x'=sqrt(sdAf_`x'^2/NAf_`x' +sdBf_`x'^2/NBf_`x')
egen avgA_`x'=mean(turnout) if year==`x' & adsl2005==5
egen avgB_`x'=mean(turnout) if year==`x' & adsl2008==0
egen avgAf_`x'=min(avgA_`x') if year==`x'
egen avgBf_`x'=min(avgB_`x') if year==`x'
gen avgdiffAB_`x'=avgAf_`x' - avgBf_`x'
}
sum avgdiff*
sum sdAB*



***************************************************
**FIGURE 5 TURNOUT BY DISTANCE TO CLOSEST UGS
**************************************************

use "${dir0}Data_national_regs.dta", clear


preserve

egen avg_distance= mean(distance_closest_SGU)
gen d_treated=0
replace d_treated=1 if distance_closest_SGU<avg_distance

bysort year d_treated: egen avg_turnout= mean(turnout)

bys year: gen avg_turnout_treated=avg_turnout if d_treated==1
bys year: replace avg_turnout_treated=avg_turnout_treated[_n-1] if d_treated==0 & avg_turnout_treated==.
bys year: replace avg_turnout_treated=avg_turnout_treated[_n+1] if d_treated==0 & avg_turnout_treated==.


bys year: gen avg_turnout_n_treated=avg_turnout if d_treated==0
bys year: replace avg_turnout_n_treated=avg_turnout_n_treated[_n-1] if d_treated==1 & avg_turnout_n_treated==.
bys year: replace avg_turnout_n_treated=avg_turnout_n_treated[_n+1] if d_treated==1 & avg_turnout_n_treated==.


collapse (mean) avg_turnout*, by (year)

label var avg_turnout "Average"
label var avg_turnout_treated "Treated (Below Median Distance UGS)"
label var avg_turnout_n_treated "Not Treated (Above Median Distance UGS)"

line avg_turnout  year, legend(row(3)) xtitle("Year") xlabel(1994 1996 2001 2006 2008 2013,valuelabel) ylabel(,nogrid) ytitle("") lcolor(gs4) lwidth(vthick) || line avg_turnout_n_treated year, lcolor(gs4) lpattern(dash) lwidth(thick) || line avg_turnout_treated year, lcolor(gs4) lpattern(shortdash_dot) lwidth(thick)

graph save "${dir3}Figure_5.gph",  replace
restore



********************************************************************************************
*Figure 5: PANEL B - Difference in Turnout between below- and above-median distance to UGS
********************************************************************************************

gen total=1 if distance_closest_SGU!=.
gen treat_median=0 if distance_closest_SGU!=.
gen ntreat_median=0 if distance_closest_SGU!=.
gen treat_decile=0 if distance_closest_SGU!=.
replace treat_median=1 if distance_closest_SGU <= 12.522
replace ntreat_median=1 if treat_median==0
replace treat_decile=1 if distance_closest_SGU <= 4.71981
keep id_city_istat_2009 turnout year treat_median treat_decile ntreat_median total
reshape wide turnout, i(id) j(year)
egen turnout_pre=rowmean(turnout1996 turnout2001)
egen turnout_post=rowmean(turnout2006-turnout2013)
local treat "total treat_median ntreat_median treat_decile"
foreach x of local treat {
egen N_`x'=total(`x')
egen a_avgturnout_pre_`x'=mean(turnout_pre) if `x'==1
egen a_sdturnout_pre_`x'=sd(turnout_pre) if `x'==1
egen a_avgturnout_post_`x'=mean(turnout_post) if `x'==1
egen a_sdturnout_post_`x'=sd(turnout_post) if `x'==1
egen avgturnout_pre_`x'=min(a_avgturnout_pre_`x')
egen avgturnout_post_`x'=min(a_avgturnout_post_`x')
egen a_1sdturnout_pre_`x'=min(a_sdturnout_pre_`x')
egen a_1sdturnout_post_`x'=min(a_sdturnout_post_`x')
gen sdturnout_pre_`x'=a_1sdturnout_pre_`x'/sqrt(N_`x')
gen sdturnout_post_`x'=a_1sdturnout_post_`x'/sqrt(N_`x')
drop a_*
}
sum avgturnout*
sum sdturnout*

******************************************
************************FIGURE 6************
******************************************

local years "1994 1996 2001 2006 2008 2013"
foreach x of local years{
clear 
use "${dir0}Data_national_regs.dta"

set matsize 1000
xi: reg turnout distance_closest_SGU pop $demo1 $demo2 i.province i.sll2001 if year==`x', cluster(province)
parmest, saving("${dir0}coeff_with_controls_`x'", replace)
clear
use "${dir0}coeff_with_controls_`x'.dta"
gen year=`x'
save "${dir0}coeff_with_controls_`x'.dta", replace
}

clear 
use "${dir0}coeff_with_controls_1994.dta"
local years "1996 2001 2006 2008 2013"
foreach x of local years{
append using "${dir0}coeff_with_controls_`x'.dta"
}

save "${dir0}coeff_with_conf_interv_reduced_form.dta", replace



clear
use "${dir0}coeff_with_conf_interv_reduced_form.dta"

keep if parm=="distance_closest_SGU"
drop if year==1994
eclplot  estimate  min95 max95 year, xtitle("Year") xlabel(1996 2001 2006 2008 2013,valuelabel) ylabel(-0.001(0.0005)0.0005,nogrid) yline(0)

graph save "${dir3}Figure_6.gph",  replace


******************************************
************FIGURE 7*********************
********************************************

clear
use "${dir0}Data_national_regs.dta"


cap drop treated*
gen treated_adsl2006=1 if adsl2006>3 & adsl2006!=.
replace treated_adsl2006=0 if adsl2006<=3

***Gen percencentiles distance to closest UGS
egen med_distSGU=pctile(distance_closest_SGU), p(50)
egen pct10_distSGU=pctile(distance_closest_SGU), p(10)
egen pct25_distSGU=pctile(distance_closest_SGU), p(25)
egen pct75_distSGU=pctile(distance_closest_SGU), p(75)

sum med_distSGU pct10_distSGU pct25_distSGU pct75_distSGU

gen treated_med_distSGU=1 if distance_closest_SGU<med_distSG
replace treated_med_distSGU=0 if distance_closest_SGU>=med_distSG & distance_closest_SGU!=.

gen treated_quart_distSGU=1 if distance_closest_SGU<pct25_distSGU
replace treated_quart_distSGU=0 if distance_closest_SGU>=pct25_distSGU & distance_closest_SGU!=.

gen treated_quart2_distSGU=1 if distance_closest_SGU>=pct25_distSGU & distance_closest_SGU<med_distSG
replace treated_quart2=0 if distance_closest_SGU<pct25_distSGU | distance_closest_SGU>=med_distSG & distance_closest_SGU!=.

gen treated_quart3_distSGU=1 if distance_closest_SGU>=med_distSGU & distance_closest_SGU<pct75_distSGU
replace treated_quart3=0 if distance_closest_SGU<med_distSGU | distance_closest_SGU>=pct75_distSGU & distance_closest_SGU!=.

gen treated_quart4_distSGU=1 if distance_closest_SGU>=pct75_distSGU & distance_closest_SGU!=.
replace treated_quart4=0 if distance_closest_SGU<pct75_distSGU

gen treated_dec_distSGU=1 if distance_closest_SGU<pct10_distSGU
replace treated_dec=0 if distance_closest_SGU>=pct10_distSGU & distance_closest_SGU!=.

gen treated_medXpost2001=treated_med*post2001
gen treated_quartXpost2001=treated_quart_distSGU*post2001
gen treated_quart2Xpost2001=treated_quart2_distSGU*post2001
gen treated_quart3Xpost2001=treated_quart3_distSGU*post2001
gen treated_quart4Xpost2001=treated_quart4_distSGU*post2001
gen treated_decXpost2001=treated_dec*post2001
gen treated_adsl2006Xpost2001=treated_adsl2006*post2001


* Placebo
cap drop post1996 treated_decXpost1996 treated_quartXpost1996 treated_medXpost1996
gen post1996=1 if year>1996
replace post1996=0 if year<=1996
gen treated_decXpost1996=treated_dec_dist*post1996
gen treated_quartXpost1996=treated_quart_dist*post1996
gen treated_quart2Xpost1996=treated_quart2_distSGU*post1996
gen treated_quart3Xpost1996=treated_quart3_distSGU*post1996
gen treated_quart4Xpost1996=treated_quart4_distSGU*post1996
gen treated_medXpost1996=treated_med_dist*post1996
gen treated_adsl2006Xpost1996=treated_adsl2006*post1996



xtreg turnout treated_med_dist treated_medXpost2001 post2001 pop   poly1a_* poly2a_* regi_* if year<2012 & year>1994,  fe  cluster(province)

preserve
parmest, saving("${dir0}coeff_with_controls_1.dta", replace)
restore


xtreg turnout treated_quart_dist treated_quart2_dist treated_quart3_dist  treated_quartXpost2001 treated_quart2Xpost2001 treated_quart3Xpost2001 post2001 pop   poly1a_* poly2a_* regi_* if year<2012 & year>1994,  fe  cluster(province)
preserve
parmest, saving("${dir0}coeff_with_controls_2.dta", replace)

clear
use "${dir0}coeff_with_controls_2.dta"
append using "${dir0}coeff_with_controls_1.dta"

keep if parm=="treated_medXpost2001"|parm=="treated_quartXpost2001"|parm=="treated_quart2Xpost2001"|parm=="treated_quart3Xpost2001"|parm=="treated_quart4Xpost2001"
save "${dir0}coeff_with_controls_diffindiff.dta", replace


gen 	group=1 if parm=="treated_medXpost2001"
replace group=2 if parm=="treated_quartXpost2001"
replace group=3 if parm=="treated_quart2Xpost2001"
replace group=4 if parm=="treated_quart3Xpost2001"
replace group=5 if parm=="treated_quart4Xpost2001"

label define gl 1 "Below Median" 2 "<25th" 3 "25th-50th" 4 "50th-75th" 
label values group gl  

eclplot  estimate  min95 max95 group, xtitle("") xtick(1(1)4) xlabel(0(1)5,valuelabel add)  ylabel(,nogrid) yline(0)

graph save "${dir3}Figure_7.gph",  replace
restore


******************************
************FIGURE 8**********
*Interest in Politics, Political Activism and Political Information by Voting Choice (2001)
******************************
use "${dir0}itanes_2001.dta", clear

* BY COALITION
gen coalition=1 if e20==2 | e20==3 | e20==8 | e20==11
replace coalition=2 if e20==4 | e20==5 | e20==9 | e20==10
replace coalition=3 if e20==6 | e20==7 | e20==12 | e20==13  | e20==14

label define coalition 1 "center-right" 2 "center-left" 3 "outsiders"
label value coalition coalition

*INTEREST IN POLITICS 

gen interest_politics=4-c1
egen z_interest_politics=std(interest_politics)

bysort coalition: sum z_interest_politics

* POLITICAL ACTIVISTM
forvalues x=1/9{
gen c7_`x'_recoded=2-c7_`x'
}

pca c7_1_recoded-c7_9_recoded
predict activism
egen z_activism =std(activism)

bysort coalition: sum z_activism


* CONSUMPTION INFO ON ELECTIONS

forvalues x=1/8{
gen d3_`x'_recoded=3-d3_`x'
}

pca d3_1_recoded-d3_8_recoded
predict info_elections

egen z_info_elections=std(info_elections)

bysort coalition: sum z_info_elections

collapse z_interest_pol z_activism z_info_elections, by(coalition)
drop if coalition==.

rename z_interest_politics z1
rename z_activism z2
rename z_info_elections z3
reshape long z, i(coalition) j(variable)

label define variable 1 "Interest in politics" 2 "Activism" 3 "Info Elections"
label value variable variable

sort variable coalition

gen variable_coalition=1 if variable==1 & coalition==1 
replace variable_coalition=2 if variable==1 & coalition==2
replace variable_coalition=3 if variable==1 & coalition==3

replace variable_coalition=5 if variable==2 & coalition==1 
replace variable_coalition=6 if variable==2 & coalition==2
replace variable_coalition=7 if variable==2 & coalition==3

replace variable_coalition=9 if variable==3 & coalition==1 
replace variable_coalition=10 if variable==3 & coalition==2
replace variable_coalition=11 if variable==3 & coalition==3

label define variable_coalition 2 "Interest in Politics" 6 "Political Activism" 10 "Political Information"
label value  variable_coalition variable_coalition

#delimit;
twoway (bar z variable_coalition if variable==1, barw(0.95))
       (bar z variable_coalition if variable==2, barw(0.95))
       (bar z variable_coalition if variable==3, barw(0.95)),
       scheme(s2mono) yscale(range(-0.02 0.37)) ylabel(0(0.1)0.35) ytitle(Standardized Score)
       legend(on) legend(rows(1)) legend(colgap(15)) legend(keygap(1)) xtitle("")
       xscale(range(0 12)) xlabel(2(4)10);


graph save "${dir3}Figure_8.gph",  replace


******************************
************FIGURE 9**********
*M5S performance and turnout
******************************

clear
use "${dir0}Data_national_regs.dta"

bysort id_city_istat_2009: egen tout0608a=mean(turnout) if year==2006 | year==2008
bysort id_city_istat_2009: egen tout0608=min(tout0608a) 
bysort id_city_istat_2009: egen tout9401a=mean(turnout) if year==2001 | year==1996 | year==1994
bysort id_city_istat_2009: egen tout9401=min(tout9401a) 
gen toutdiff =  tout0608 - tout9401
pwcorr toutdiff abs_votes_M5S, sig
codebook abs_votes_M5S
* Scatter plot
scatter toutdiff abs_votes_M5S if year==2013, xline(.169806 .238996 .295) || lfit toutdiff abs_votes_M5S if year==2013
graph save   "${dir3}Figure_9.gph",  replace




***APPENDIX

******************************
************FIGURE A1*********
******************************
clear
use "${dir0}Data_national_regs.dta"
keep if year>1994
reg logpop dumyear*, robust
predict resid_logpop, resid

*Top panel
preserve
keep if distance_closest_SGU <3
bysort year: egen mean_log_pop_res=mean(resid_logpop)
line mean_log_pop_res  year, legend(row(3)) xtitle("Year") xlabel(1996 2001 2006 2008 2013,valuelabel) ylabel(2.15(0.05)2.2,valuelabel add) ytitle("") lcolor(gs4) lwidth(vthick) 
graph save   "${dir3}Figure_A1_top.gph",  replace
restore

*Bottom panel
preserve
keep if distance_closest_SGU >30
bysort year: egen mean_log_pop_res=mean(resid_logpop)
line mean_log_pop_res  year, legend(row(3)) xtitle("Year") xlabel(1996 2001 2006 2008 2013,valuelabel) ylabel(-0.65(0.05)-0.4,valuelabel add) ytitle("") lcolor(gs4) lwidth(vthick) 
graph save   "${dir3}Figure_A1_bottom.gph",  replace
restore



******************************
************FIGURE A2********
******************************

clear
use "${dir0}Data_national_regs.dta"

cap drop treated*
gen treated_adsl2006=1 if adsl2006>3 & adsl2006!=.
replace treated_adsl2006=0 if adsl2006<=3

***Gen percencentiles distance to closest UGS
egen med_distSGU=pctile(distance_closest_SGU), p(50)
egen pct10_distSGU=pctile(distance_closest_SGU), p(10)
egen pct25_distSGU=pctile(distance_closest_SGU), p(25)
egen pct75_distSGU=pctile(distance_closest_SGU), p(75)

gen treated_med_distSGU=1 if distance_closest_SGU<med_distSG
replace treated_med_distSGU=0 if distance_closest_SGU>=med_distSG & distance_closest_SGU!=.

gen treated_quart_distSGU=1 if distance_closest_SGU<pct25_distSGU
replace treated_quart_distSGU=0 if distance_closest_SGU>=pct25_distSGU & distance_closest_SGU!=.

gen treated_quart2_distSGU=1 if distance_closest_SGU>=pct25_distSGU & distance_closest_SGU<med_distSG
replace treated_quart2=0 if distance_closest_SGU<pct25_distSGU | distance_closest_SGU>=med_distSG & distance_closest_SGU!=.

gen treated_quart3_distSGU=1 if distance_closest_SGU>=med_distSGU & distance_closest_SGU<pct75_distSGU
replace treated_quart3=0 if distance_closest_SGU<med_distSGU | distance_closest_SGU>=pct75_distSGU & distance_closest_SGU!=.

gen treated_quart4_distSGU=1 if distance_closest_SGU>=pct75_distSGU & distance_closest_SGU!=.
replace treated_quart4=0 if distance_closest_SGU<pct75_distSGU

gen treated_dec_distSGU=1 if distance_closest_SGU<pct10_distSGU
replace treated_dec=0 if distance_closest_SGU>=pct10_distSGU & distance_closest_SGU!=.

gen treated_medXpost2001=treated_med*post2001
gen treated_quartXpost2001=treated_quart_distSGU*post2001
gen treated_quart2Xpost2001=treated_quart2_distSGU*post2001
gen treated_quart3Xpost2001=treated_quart3_distSGU*post2001
gen treated_quart4Xpost2001=treated_quart4_distSGU*post2001
gen treated_decXpost2001=treated_dec*post2001
gen treated_adsl2006Xpost2001=treated_adsl2006*post2001

* Placebo
cap drop post1996 treated_decXpost1996 treated_quartXpost1996 treated_medXpost1996
gen post1996=1 if year>1996
replace post1996=0 if year<=1996
gen treated_decXpost1996=treated_dec_dist*post1996
gen treated_quartXpost1996=treated_quart_dist*post1996
gen treated_quart2Xpost1996=treated_quart2_distSGU*post1996
gen treated_quart3Xpost1996=treated_quart3_distSGU*post1996
gen treated_quart4Xpost1996=treated_quart4_distSGU*post1996
gen treated_medXpost1996=treated_med_dist*post1996
gen treated_adsl2006Xpost1996=treated_adsl2006*post1996



xtreg turnout treated_med_distSGU treated_medXpost1996 post1996 pop   poly1a_* poly2a_* dumyear1* if year<2002 & year>=1994,  fe  cluster(province)
preserve
parmest, saving("${dir0}coeff_with_controls_3", replace)
restore


xtreg turnout treated_quart_dist treated_quart2_dist treated_quart3_dist  treated_quartXpost1996 treated_quart2Xpost1996 treated_quart3Xpost1996 post1996 pop   poly1a_* poly2a_* regi_* if year<2002 & year>1994,  fe  cluster(province)
preserve
parmest, saving("${dir0}coeff_with_controls_4", replace)

clear
use 		 "${dir0}coeff_with_controls_3.dta"
append using "${dir0}coeff_with_controls_4.dta"

keep if parm=="treated_medXpost1996"|parm=="treated_quartXpost1996"|parm=="treated_quart2Xpost1996"|parm=="treated_quart3Xpost1996"|parm=="treated_quart4Xpost1996"


append using "${dir0}coeff_with_controls_diffindiff.dta"
save 	     "${dir0}coeff_with_controls_diffindiff_placebo.dta", replace


gen 	group=1 if parm=="treated_medXpost2001"
replace group=3 if parm=="treated_medXpost1996"

replace group=8 if parm=="treated_quartXpost2001"
replace group=10 if parm=="treated_quartXpost1996"

replace group=15 if parm=="treated_quart2Xpost2001"
replace group=17 if parm=="treated_quart2Xpost1996"


replace group=22 if parm=="treated_quart3Xpost2001"
replace group=24 if parm=="treated_quart3Xpost1996"

replace group=27 if parm=="treated_quart4Xpost2001"
replace group=29 if parm=="treated_quart4Xpost1996"

sort group


label define gl 2 "Below Median" 9 "<25th" 16 "25th-50th" 23 "50th-75th" 
label values group gl  

eclplot  estimate  min95 max95 group, xtitle("")  xlabel(-5(7)30,valuelabel add)  ylabel(,nogrid) 

graph save   "${dir3}Figure_A2.gph",  replace

restore


clear
local years "1994 1996 2001 2006 2008 2013 1 2 3 4"
foreach x of local years{
clear
erase "${dir0}coeff_with_controls_`x'.dta"
}

erase "${dir0}coeff_with_conf_interv_reduced_form.dta"
erase "${dir0}coeff_with_controls_diffindiff.dta"
erase "${dir0}coeff_with_controls_diffindiff_placebo.dta"
clear
